﻿<?php
class Todo extends AppModel {
	public $name = "Todo";
	public $useTable = "ac_todo";
	public $validate = array();

	public function cleanData() {
		if (isset($this->data['Todo']['title'])) {
			$this->data['Todo']['title'] = trim($this->data['Todo']['title']);
		}
		if (isset($this->data['Todo']['note'])) {
			$this->data['Todo']['note'] = trim($this->data['Todo']['note']);
		}
		if (isset($this->data['Todo']['comment'])) {
			$this->data['Todo']['comment'] = trim($this->data['Todo']['comment']);
		}

		//print_r ($this->data['Todo']); exit();
	}

	public function beforeValidate($options = array()) {
		parent::beforeValidate($options);
		$this->cleanData();

		$this->validate = array(
			'title' => array(
				'require' => array(
					'rule' => array('notEmpty'),
					'message' => 'タイトルは必ず入力する必要があります。'
				),
				'max_length' => array(
					'rule' => array('checkTitleLength'),
					'message' => 'タイトルは64文字以内で入力して下さい。'
				)
			),
			'end_date' => array(
				'require' => array(
					'rule' => array('notEmpty'),
					'message' => '期限は必ず入力する必要があります。'
				),
				/*
				'valid' => array(
					'rule' => array('checkValidExpiredDate'),
					'message' => '譛滄剞縺ｮ謖・ｮ壹′荳肴ｭ｣縺ｧ縺吶・'
				) */
			),
			'piority' => array(
				'required' => true
			),
			'note' => array(
				'rule' => array('checkNoteLength'),
				//'allowEmpty' => true,
				'message' => '内容は512文字以内で入力して下さい。'
			),
			'comment' => array(
				'rule' => array('checkCommentLength'),
				//'allowEmpty' => true,
				'message' => 'コメントは64文字以内で入力して下さい。'
			)
		);

		return true;
	}

	public function setLogin($login) {
		$this->data['Todo']['login'] = $login;
	}

	public function setEndDate($end_date) {

		$hour = $end_date['hour'];
		$minute = $end_date['minute'];
		$year = $end_date['year'];
		$month = $end_date['month'];
		$day = $end_date['day'];

		$this->data['Todo']['end_date'] = date('Y-n-j H:i:0',mktime($hour, $minute, 0, $month, $day, $year));

	}

	public function setCreateTime() {
		$this->data['Todo']['insert_time'] = date('Y-n-j H:i:0');
		$this->data['Todo']['update_time'] = date('Y-n-j H:i:0');
	}

	public function setUpdateTime() {
		$this->data['Todo']['update_time'] = date('Y-n-j H:i:0');
	}

	public function setCompleteFlag($value = null) {
		if ($value) {
			$this->data['Todo']['complete_flag'] = $value;
		}
	}

	public function checkValidExpiredDate() {
        if (isset($this->data['Todo']['end_date'])) {
			return (strtotime($this->data['Todo']['end_date']) > time()) ? true : false;
		}
	}

	public function checkNoteLength() {
		if (isset($this->data['Todo']['note'])) {
			return (mb_strlen($this->data['Todo']['note'], 'UTF-8') <= 512) ? true : false;
		}
	}

	public function checkCommentLength() {
		if (isset($this->data['Todo']['comment'])) {
			return (mb_strlen($this->data['Todo']['comment'], 'UTF-8') <= 64) ? true : false;
		}
	}

	public function checkTitleLength() {
		if (isset($this->data['Todo']['title'])) {
			return (mb_strlen($this->data['Todo']['title'], 'UTF-8') <= 64) ? true : false;
		}
	}
}
?>